<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="cpItemList"
      />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editFormResult" :disabled="!cpEnabledResult"  v-if="cpShowResult">
        <template #content>
          <div id="box-result" class="box-title"  >{{cpBusTitle}}结果</div>

          <customFormItem label="通过" name="flagPass" :parent="_self" :form="form"  :type="constCommon.formItemType.switch.name" />
          <customFormItem label="费用" name="cost" :parent="_self" :form="form"  :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="完成时间" name="endTime" :parent="_self" :form="form"
                          :type="constCommon.formItemType.datePicker.name"
                          :config="{'enable-time-picker': true,isWithHmsForm: true}"
          />

          <div id="box-report" class="box-title" >{{cpBusTitle}}报告</div>
          <customFormItem
            label=""
            name="workFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id + '_report', fileRefTable: 'device_plan_task', fileSavePath: 'device_plan_task'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存主单据后再上传"
                :max="20"
                :disabled="propReadonly"
                multiple
              />
            </template>
          </customFormItem>
        </template>
      </customForm>

      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly || cpEnabledResult">
        <template #content>
          <div id="box-base" class="box-title">{{cpBusTitle}}任务</div>
          <customFormItem label="任务名称" name="taskName" :parent="_self" :form="form" />
          <customFormItem label="任务单号" name="busKey" :parent="_self" :form="form" disabled/>
          <customFormItem label="开始时间" name="startTime" :parent="_self" :form="form"
                          :type="constCommon.formItemType.datePicker.name" disabled  :config="{'enable-time-picker': true,isWithHmsForm: true}"/>
          <customFormItem label="执行人" name="busUser" :parent="_self" :form="form"  disabled/>

          <customFormItem label="备注" name="remark" :parent="_self" :form="form"/>

          <div id="box-exec" class="box-title">{{cpBusTitle}}计划</div>
          <customFormItem
            label="计划单号"
            name="planKey"
            :parent="_self"
            :form="form"
            disabled
          />
          <customFormItem
            label="计划名称"
            name="planName"
            :parent="_self"
            :form="form"
            disabled
          />
          <customFormItem
            label="科室"
            name="deptName"
            :parent="_self"
            :form="form"
            disabled
          />
          <customFormItem label="计划开始日期" name="dateBegin" :parent="_self" :form="form" disabled/>
          <customFormItem label="计划结束日期" name="dateEnd" :parent="_self" :form="form"  disabled/>
          <customFormItem label="周期类型" name="cycleType" :parent="_self" :form="form"
                          :type="constCommon.formItemType.selectDict.name"
                          :config="{dictType: constCommon.dictType.devicePlanCycleType}"
                          disabled
          />
          <customFormItem label="每周开始" name="cycleBegin" :parent="_self" :form="form" v-show="form.cycleType == constCommon.devicePlanCycleType.week"
                :type="constCommon.formItemType.inputNumber.name"
                :config="{min: 1, max: 7, format: (value) => `周${value}`}"
                          disabled
          />
          <customFormItem label="每月开始" name="cycleBegin" :parent="_self" :form="form"  v-show="form.cycleType == constCommon.devicePlanCycleType.month"
                :type="constCommon.formItemType.inputNumber.name"
                :config="{min: 1, max: 31, format: (value) => `${value}号`}"
                          disabled
          />
          <customFormItem label="每年开始" name="cycleBegin" :parent="_self" :form="form"  v-show="form.cycleType == constCommon.devicePlanCycleType.year"
              :type="constCommon.formItemType.datePicker.name"
                          disabled
          />

          <customFormItem label="周期完成期限" name="cycleDay" :parent="_self" :form="form"
                :type="constCommon.formItemType.inputNumber.name"
                disabled
          />

          <div id="box-device" class="box-title">设备信息</div>
          <customFormItem label="设备名称" name="deviceName" :parent="_self" :form="form"  disabled />
          <customFormItem label="设备编码" name="deviceCode" :parent="_self" :form="form"  disabled />
          <customFormItem label="标签码" name="deviceSn" :parent="_self" :form="form"  disabled />
          <customFormItem label="规格型号" name="deviceSpec" :parent="_self" :form="form"  disabled />
          <customFormItem label="规格型号" name="deviceSpec" :parent="_self" :form="form"  disabled />



          <div id="box-file" class="box-title">{{cpBusTitle}}附件</div>
          <customFormItem
            label=""
            name="workFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id, fileRefTable: 'device_plan_task', fileSavePath: 'device_plan_task'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存主单据后再上传"
                :max="20"
                :disabled="propReadonly"
                multiple
              />
            </template>
          </customFormItem>
        </template>
      </customForm>
    </div>

    <!--计划选择选择-->
    <tableDrawer ref="tableDrawerPlan" refName="tableDrawerPlan" viewKey="device_work_item_select" attach="body"
                 loadForce
                 :queryWrapper="$newWrapper().eq('supplier_id', strings.defaultIfBlank(form.supplierId, '-1'))"
                 :propUserParam="{busType: cpBusType}"
                 subtitle=""
    />
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from "@/components/custom/form/customUpload.vue";

export default {
  desc: '设备工作表-详情',
  data() {
    return {
      form: {},
    }
  },
  computed: {
    // 导航列表
    cpItemList() {
      if (!this.cpShowResult) {
        return [
          { name: 'box-base', title: `${this.cpBusTitle}任务` },
          { name: 'box-exec', title: `${this.cpBusTitle}计划` },
          { name: 'box-device', title: `设备信息` },
          { name: 'box-file', title: `${this.cpBusTitle}附件` },
        ]
      }
      return [
        { name: 'box-result', title: `${this.cpBusTitle}结果` },
        { name: 'box-report', title: `${this.cpBusTitle}报告` },
        { name: 'box-base', title: `${this.cpBusTitle}任务` },
        { name: 'box-exec', title: `${this.cpBusTitle}计划` },
        { name: 'box-device', title: `设备信息` },
        { name: 'box-file', title: `${this.cpBusTitle}附件` },
      ]
    },
    // 业务类型
    cpBusType() {
      return this.propParam && this.propParam.busType ? this.propParam.busType : -1
    },
    // 业务标题
    cpBusTitle() {
      return this.utils.getDictLabel(this.cpBusType, this.constCommon.dictType.devicePlanTaskBusType, null, false)
    },
    // 明细列表配置标志
    cpItemVIewKey() {
      return  'device_plan_item'
    },
    // 结果可显示
    cpShowResult() {
      return  'start' != this.form.flowStatus
    },
    // 结果可编辑
    cpEnabledResult() {
      return  'execBegin' == this.form.flowStatus
    },
  },
  created() {
    let that = this
    this.$emit('event-register', {
      //表单校验
      validate: (callback) => {
        if (that.cpEnabledResult) {
          return this.$refs.editFormResult.validate(callback)
        }
        return this.$refs.editForm.validate(callback)
      },

      //保存前操作
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },

      //数据加载后
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        if (this.propParent.cpFormNew) {
          this.$set(this.form, 'busUser', this.utils.getCurrentUser().name)
          this.$set(this.form, 'deptId', this.utils.getCurrentUser().deptId)
          this.$set(this.form, 'deptName', this.utils.getCurrentUser().deptName)
          this.$set(this.form, 'busDate', new Date().format(this.dates.defaultDatePattern))
          this.$set(this.form, 'busType', this.cpBusType)
          this.$set(this.form, 'busKey', '自动获取')
        }
        this.loadDtl()
      },

      //数据保存后
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },

      //数据删除后
      afterDelete: () => {
      },

      //流程提交前
      beforeFlow: (flowButtonKey) => {
        return true
      },

      //流程提交后
      afterFlow: (flowButtonKey) => {
      },

      //表单关闭前
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    //上级对象
    propParent: Object,
    //上级初始化数据
    propInitData: Object,
    //主键字段
    propPrimaryKey: String,
    //数据字典
    propDictionaries: Object,
    //当前操作行
    propCurrentRow: Object,
    //表单数据
    propViewFormData: Object,
    //是否只读
    propReadonly: Boolean,
    //操作状态
    propStatus: String,
    //表单规则
    propRules: Object,
    propParam: Object,
  },
  components: {
    customUpload,
    customAnchor
  },
  methods: {
    // 加载明细
    loadDtl() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('planId', this.form.id)
      } else {
        queryWrapper.eq('planId', -1)
      }
      this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },

    // 明细 新增、删除、修改后调用
    afterTableDataChange() {
      this.utils.refreshRefs(this)
    },
    // 科室选择
    deptNameChange(item) {
      this.$set(this.form, 'deptId', item.id)
      this.$set(this.form, 'deptName', item.name)
    },

    // 选择完计划
    tableDrawerPlanConfirm(row) {
      //转换属性
      rows.forEach(ob => {
        ob.workId = this.form.id
        ob.workItemId = ob.id
        ob.id = null
      })

      //批量添加
      this.utilVue.loadShow(true)
      this.$ajax.post('/device/work_item/saveBatch', rows).then(resp => {
        this.utilVue.loadHide()
        if (resp.code == 0) {
          this.utilMessage.success(resp.msg)
          this.loadDtl()
        }
      })
    }
  }
}
</script>
